
class Node {
    int val;
    Node next;
    Node(int val) {
        this.val = val;
        this.next = null;
    }
}

public class Test24 {
    public static Node reverseList(Node head) {
        Node prev = null;
        Node current = head;
        while (current != null) {
            Node nextTemp = current.next;
            current.next = prev;
            prev = current;
            current = nextTemp;
        }
        return prev;
    }

    public static void main(String[] args) {
        // 创建链表 1 → 2 → 3 → null
        Node node1 = new Node(1);
        Node node2 = new Node(2);
        Node node3 = new Node(3);
        node1.next = node2;
        node2.next = node3;

        // 反转链表
        Node newHead = reverseList(node1);

        // 输出反转后的链表
        while (newHead != null) {
            System.out.print(newHead.val + " → ");
            newHead = newHead.next;
        }
        System.out.println("null");
    }
}